package com.hllxd.hism.star.basic.service.service;

import com.hllxd.hism.star.basic.service.collect.DateUtils;
import com.hllxd.hism.star.basic.service.dao.mapper.doris.NoOwnerTableInfoMapper;
import com.hllxd.hism.star.basic.service.model.dapan.DapanDimDictDefine;
import com.hllxd.hism.star.basic.service.model.doris.NoOwnerTableInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wuguangxi
 * @description
 * @date 2023/9/15 11:14
 **/
@Service
public class NoOwnerTableInfoServiceImpl implements NoOwnerTableInfoService {

    @Autowired
    private NoOwnerTableInfoMapper mapper;
    @Autowired
    private DimDefineManagerService defineManagerService;

    private static final String TYPE_CODE = "存储白名单-未设置表负责人";


    @Override
    public List<NoOwnerTableInfo> findByParams(NoOwnerTableInfo params) {
        // 格式化日期
        params.setDt(params.getDt() + " 00:00:00");
        List<NoOwnerTableInfo> list = mapper.findListByParams(params);
        List<DapanDimDictDefine> dictDefines =defineManagerService.searchDimDefine(TYPE_CODE);
        Map<String, Long> tableIdMap = dictDefines.stream()
                .filter(dic ->dic.getItemKey() != null)
                .collect(Collectors.toMap(DapanDimDictDefine::getItemKey, DapanDimDictDefine::getId, (v1, v2) -> v1));
        list.forEach(tableInfo -> {
            if(tableIdMap.containsKey(tableInfo.getTableId().toString())){
                tableInfo.setPermitTableId(tableIdMap.get(tableInfo.getTableId().toString()).toString());
            }
        });
        return list;
    }

    @Override
    public Long add2PermitList(Integer tableId, String tableName) {
        DapanDimDictDefine define = new DapanDimDictDefine();
        define.setTypeCode(TYPE_CODE);
        define.setTypeDesc("-");
        define.setItemKey(tableId.toString());
        define.setItemValue(DateUtils.formatDate(System.currentTimeMillis(), "yyyyMMdd"));
        define.setItemDesc(tableName);
        define.setOwnerId("");
        byte a = 0;
        define.setAction(a);
        define.setCreateTime(DateUtils.getDateTime());
        return defineManagerService.setDimDefine(define);
    }

    @Override
    public void removePermitList(Long id) {
        defineManagerService.delDimDefine(id);
    }
}
